package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/12/15  21:21
 *
 */
@Service
public class SupplierServiceImpl  implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

//    分页展示物资来源信息列表
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        PageHelper.startPage(pageNum, pageSize);
        Example o = new Example(Supplier.class);
        Example.Criteria criteria=o.createCriteria();
        if (!StringUtils.isEmpty(supplierVO.getName())) {
            criteria.andLike("name", "%" + supplierVO.getName() + "%");
        }

        if (!StringUtils.isEmpty(supplierVO.getContact())) {
            criteria.andLike("contact", "%" + supplierVO.getContact() + "%");
        }

        if (!StringUtils.isEmpty(supplierVO.getAddress())) {
            criteria.andLike("address", "%" + supplierVO.getAddress() + "%");
        }
        List<Supplier> suppliers = supplierMapper.selectByExample(o);
        //转vo
        List<SupplierVO> supplierVOS = SupplierConverter.converterToSupplierVOList(suppliers);
        PageInfo<Supplier> info = new PageInfo<>(suppliers);
        return new PageVO<>(info.getTotal(), supplierVOS);
    }

//    添加物资来源信息
    @Override
    public void add(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }


//    编辑页，物资来源数据回显

    @Override
    public SupplierVO edit(Long id) {
        return SupplierConverter.converterToSupplierVO(supplierMapper.selectByPrimaryKey(id));
    }

//    编辑页，数据提交，修改物资来源信息

    @Override
    public void update(Long id, Supplier supplier) {
        supplier.setId(id);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

//    删除物资来源信息

    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

//    添加入库记录页面回显物资来源信息
    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }
}
